package tagdb.ui.stats;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.data.statistics.HistogramDataset;
import org.jfree.data.statistics.HistogramType;
import tagdb.denovo.DenovoHit;
import tagdb.model.DenovoSearchResult;
import tagdb.model.Result;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>Class to plot a histogram of the number of peaks of all intensity values.</p>
 *
 * @author T.Muth
 */
public class DenovoScoreHistogram extends Chart {
    private double[] data;
    private String filename;

    /**
     * Constructs a denovo score histogram
     *
     * @param resultData
     */
    public DenovoScoreHistogram(Result resultData) {
        super(resultData);
    }

    @Override
    protected void process(Result resultData) {
        // List of all the scores
        List<Double> scores = new ArrayList<Double>();
        List<List<DenovoHit>> hitList = new ArrayList<List<DenovoHit>>(((DenovoSearchResult) resultData).getResultMap().values());
        for (List<DenovoHit> hits : hitList) {
            for (DenovoHit hit : hits) {
                scores.add(hit.getPepNovoScore());
            }
        }

        // Set data.
        data = new double[scores.size()];
        for (int i = 0; i < scores.size(); i++) {
            data[i] = scores.get(i);
        }

        // Set filename.
        filename = resultData.getFilename();

        setChart();
    }

    @Override
    protected void setChart() {
        HistogramDataset dataset = new HistogramDataset();
        dataset.setType(HistogramType.FREQUENCY);
        dataset.addSeries(filename, data, 40);
        chart = ChartFactory.createHistogram(getChartTitle(),
                "Score",
                "Frequency",
                dataset,
                PlotOrientation.VERTICAL,
                true,
                true,
                false);


        XYPlot plot = (XYPlot) chart.getPlot();
        plot.setBackgroundAlpha(0f);
        plot.setDomainGridlinesVisible(false);
        plot.setRangeGridlinesVisible(false);
        plot.setOutlineVisible(false);
        
        XYBarRenderer renderer = new XYBarRenderer();
        renderer.setShadowVisible(false);
        plot.setRenderer(renderer);
    }

    @Override
    public String getChartTitle() {
        return "De Novo Score Histogram";
    }
}
